﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class PhieuDatCom : Form
    {
        public PhieuDatCom()
        {
            InitializeComponent();

        }

        private void label11_Click(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void PhieuDatCom_Load(object sender, EventArgs e)
        {
            LoadTenKhachHangCombobox();
            LoadTenNhanVienDatCombobox();
            LoadTenNhanVienGiao();
            LoadTenKhuyenMaiCombobox();
            LoadTenMonAnCombobox();
            this.cbtrangthai.SelectedIndex = 0;
            cbmonan.SelectedIndex = 1;
            cbkhachhang.SelectedIndex = 1;
            this.cbngaydat.Value = DateTime.Now;
            this.cbngaygiao.Value = DateTime.Now;
            dtpgiodat.Value = DateTime.Now;
            dtpgiogiao.Value = DateTime.Now;
        }
        public void LoadTenKhachHangCombobox()
        {
            KhachHangBUS pdcBUS = new KhachHangBUS();
            List<KhachHangDTO> listKH = new List<KhachHangDTO>();
            listKH = pdcBUS.LayDanhSachKhachHang();
            this.cbkhachhang.DataSource = listKH;
            cbkhachhang.DisplayMember = "TenKhachHang";
            cbkhachhang.ValueMember = "ID_KhachHang";
            
        }
        public void LoadTenKhuyenMaiCombobox()
        {
            KhuyenMaiBUS kmBUS = new KhuyenMaiBUS();
            List<KhuyenMaiDTO> listKM = new List<KhuyenMaiDTO>();
            listKM = kmBUS.LayDanhSachKhuyenMaiBUS();
            this.cbkhuyenmai.DataSource = listKM;
            this.cbkhuyenmai.DisplayMember = "TenKhuyenMai";
            cbkhuyenmai.ValueMember = "ID_KhuyenMai";
        }
        public void LoadTenNhanVienDatCombobox()
        {
            NhanVienBUS nvBUS = new NhanVienBUS();
            List<NhanVienDTO> listNV = new List<NhanVienDTO>();
            listNV = nvBUS.LayDanhSachNhanVienBUS();
            cbnhanviendat.DataSource = listNV;
            cbnhanviendat.DisplayMember = "TenNhanVien";
            cbnhanviendat.ValueMember = "ID_NhanVien";
        }
        public void LoadTenNhanVienGiao()
        {
            NhanVienBUS nvBUS = new NhanVienBUS();
            List<NhanVienDTO> listNV = new List<NhanVienDTO>();
            listNV = nvBUS.LayDanhSachNhanVienBUS();
            cbnhanviengiao.DataSource = listNV;
            cbnhanviengiao.DisplayMember = "TenNhanVien";
            cbnhanviengiao.ValueMember = "ID_NhanVien";
        }
        public void LoadTenMonAnCombobox()
        {
            MonAnBUS maBUS = new MonAnBUS();
            List<MonAnDTO> listMA = new List<MonAnDTO>();
            listMA = maBUS.LayDanhSachMonAn();
            cbmonan.DataSource = listMA;
            cbmonan.DisplayMember = "TenMonAn";
            cbmonan.ValueMember = "ID_MonAn";
        }
        public void LoadGia()
        {
            MonAnBUS maBUS = new MonAnBUS();
            List<MonAnDTO> listMA = new List<MonAnDTO>();
            String maMA = this.cbmonan.SelectedValue.ToString();
            try
            {
                listMA = maBUS.LayGiaTheoMaMonAn(int.Parse(maMA));
                MonAnDTO maDTO = new MonAnDTO();

                maDTO = listMA[0];

                //String ID_MonAn = maDTO.ID_MonAn.ToString();
                //String TenMonAn = maDTO.TenMonAn.ToString();
                String Gia = maDTO.Gia.ToString();

                txtgia.Text = Gia;

            }
            catch (Exception ex)
            {
                //MessageBox.Show("Lỗi dữ liệu load giá " + ex.Message);
            }
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        public void XemDanhSachPhieuDatCom()
        {
            ///CẤU HÌNH MẶC ĐỊNH = 0
            int chedo = CauHinh.chedo;
            ///END CẤU HÌNH
            PhieuDatComBUS pdcBUS = new PhieuDatComBUS();

            List<PhieuDatComDTO> listPDC = new List<PhieuDatComDTO>();
            List<KhachHangDTO> listKH = new List<KhachHangDTO>();
            try
            {
                listPDC = pdcBUS.XemDanhSacPhieuDatCom_DirtyRead(chedo);
                this.dgvdsphieudatcom.Rows.Clear();
                
                for (int i = 0; i < listPDC.Count; i++)
                {
                    KhachHangBUS khBUS = new KhachHangBUS();
                    PhieuDatComDTO pdcDTO = new PhieuDatComDTO();
                    KhachHangDTO khDTO = new KhachHangDTO();
                    
                    pdcDTO = listPDC[i];

                    listKH = khBUS.LayDanhSachKhachHangTheoMa(pdcDTO.ID_KhachHang);
                    khDTO = listKH[0];

                    String ID_PhieuDatCom = pdcDTO.ID_PhieuDatCom.ToString();
                    String ID_KhachHang = pdcDTO.ID_KhachHang.ToString();
                   // khDTO = khBUS.LayDanhSachKhachHangTheoMa2(pdcDTO.ID_KhachHang);
                    String TenKhachHang = khDTO.TenKhachHang;
                    String NgayDat = pdcDTO.NgayDat.ToShortDateString();
                    String GioDat = pdcDTO.GioDat.ToString();
                    String NgayGiao = pdcDTO.NgayGiao.ToShortDateString();
                    String GioGiao = pdcDTO.GioGiao.ToString();
                    String TongTien = pdcDTO.TongTien.ToString();
                    String TrangThai = pdcDTO.TrangThai.ToString();
                    String DiaChi = khDTO.DiaChi;
                    String GhiChu = pdcDTO.GhiChu.ToString();
                    dgvdsphieudatcom.Rows.Add(ID_PhieuDatCom, TenKhachHang, NgayDat, GioDat, NgayGiao, GioGiao,DiaChi,GhiChu, TongTien, TrangThai);

                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Lỗi dữ liệu");
                MessageBox.Show(ex.ToString());
            }
        }
        private void txtxemds_Click(object sender, EventArgs e)
        {
            int loi = CauHinh.tenloi;
            if (loi == 0)//1. Dirty Read
            {
                XemDanhSachPhieuDatCom();
            }
            else if(loi == -1)
            {
                MessageBox.Show("Chưa chọn cấu hình lỗi");
            }
        }

        public void ThemPhieuDatCom()
        {
            //CẦU HÌNH
            int chedo = CauHinh.chedo; //= 0
            //CẤU HÌNH
            string ma = cbkhuyenmai.SelectedValue.ToString();
            try
            {
                PhieuDatComBUS pdcBUS = new PhieuDatComBUS();
                PhieuDatComDTO pdcDTO = new PhieuDatComDTO();

                int mapdcmax = pdcBUS.LayMaPhieuDatComMax();
                pdcDTO.ID_PhieuDatCom = mapdcmax + 1;
                pdcDTO.NgayDat = this.cbngaydat.Value;
                pdcDTO.NgayGiao = cbngaygiao.Value;
                pdcDTO.GioDat = TimeSpan.Parse(dtpgiodat.Text);
                pdcDTO.GioGiao = TimeSpan.Parse(dtpgiogiao.Text);
                pdcDTO.ID_NhanVienDat = int.Parse(cbnhanviendat.SelectedValue.ToString());
                pdcDTO.ID_NhanVienGiao = int.Parse(this.cbnhanviengiao.SelectedValue.ToString());
                pdcDTO.ID_KhachHang = int.Parse(this.cbkhachhang.SelectedValue.ToString());
                pdcDTO.ID_KhuyenMai = int.Parse(this.cbkhuyenmai.SelectedValue.ToString());
                pdcDTO.GhiChu = this.txtghichu.Text;
                pdcDTO.TongTien = int.Parse(this.lbconlai.Text);
                pdcDTO.TrangThai = cbtrangthai.SelectedIndex;
                string kq = pdcBUS.ThemPhieuDatCom_DirtyRead(pdcDTO, chedo,int.Parse(ma));
                if (kq == "")
                {
                    MessageBox.Show("Thêm thành công!");
                    ThemCTPC();
                    CatNhatNguyenLieu();
                }
                else MessageBox.Show(kq);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi - " + ex.Message);
            }
        }

        public void ThemPhieuDatCom_LostUpdate()
        {
            //CẦU HÌNH
            int chedo = CauHinh.chedo; //= 0
            //CẤU HÌNH
            //string ma = cbkhuyenmai.SelectedValue.ToString();
            try
            {
                PhieuDatComBUS pdcBUS = new PhieuDatComBUS();
                PhieuDatComDTO pdcDTO = new PhieuDatComDTO();

                int mapdcmax = pdcBUS.LayMaPhieuDatComMax();
                pdcDTO.ID_PhieuDatCom = mapdcmax + 1;
                pdcDTO.NgayDat = this.cbngaydat.Value;
                pdcDTO.NgayGiao = cbngaygiao.Value;
                pdcDTO.GioDat = TimeSpan.Parse(dtpgiodat.Text);
                pdcDTO.GioGiao = TimeSpan.Parse(dtpgiogiao.Text);
                pdcDTO.ID_NhanVienDat = int.Parse(cbnhanviendat.SelectedValue.ToString());
                pdcDTO.ID_NhanVienGiao = int.Parse(this.cbnhanviengiao.SelectedValue.ToString());
                pdcDTO.ID_KhachHang = int.Parse(this.cbkhachhang.SelectedValue.ToString());
                pdcDTO.ID_KhuyenMai = int.Parse(this.cbkhuyenmai.SelectedValue.ToString());
                pdcDTO.GhiChu = this.txtghichu.Text;
                pdcDTO.TongTien = int.Parse(this.lbconlai.Text);
                pdcDTO.TrangThai = cbtrangthai.SelectedIndex;
                string kq = pdcBUS.ThemPhieuDatCom_LostUpdate(pdcDTO, chedo);
                if (kq == "")
                {
                    MessageBox.Show("Thêm thành công!");
                    ThemCTPC(CauHinh.loi);
                    CatNhatNguyenLieu(CauHinh.loi);
                }
                else MessageBox.Show(kq);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi - " + ex.Message);
            }
        }

        private void btlapphieu_Click(object sender, EventArgs e)
        {
            int Count = this.dgvchitietdatmon.RowCount;


            int loi = CauHinh.tenloi;
            if (loi == -1)
            {
                MessageBox.Show("Chưa chọn cấu hình lỗi");
            }
            else if (Count ==0)
            {
                MessageBox.Show("Chưa chọn món ăn. Vui lòng chọn lại!");
            }
            else if(loi == 0) //1. Dirtyread
            {
                ThemPhieuDatCom();
            }
            else if (loi == 3) //4. LostUpdate
            {
                ThemPhieuDatCom_LostUpdate();
            }
        }

        private void cbkhachhang_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadComboboxKhachhang();
        }
        public void LoadComboboxKhachhang()
        {
            KhachHangBUS khBUS = new KhachHangBUS();
            List<KhachHangDTO> listKH = new List<KhachHangDTO>();
            String maKh=cbkhachhang.SelectedValue.ToString();
            try
            {
                listKH = khBUS.LayDanhSachKhachHangTheoMa(int.Parse(maKh));
                    KhachHangDTO khDTO = new KhachHangDTO();

                    khDTO = listKH[0];

                    String ID_KhachHang = khDTO.ID_KhachHang.ToString();
                    String TenKhachHang = khDTO.TenKhachHang.ToString();
                    String DienThoai = khDTO.DienThoai.ToString();
                    String Email = khDTO.Email.ToString();
                    String DiaChi = khDTO.DiaChi.ToString();
                    String CongTy = khDTO.CongTy.ToString();

                    txtdienthoai.Text = DienThoai;
                    txtemail.Text = Email;
                    txtdiachi.Text = DiaChi;
                    txtcongty.Text = CongTy;
                    
            }
            catch (Exception ex)
            {
                //MessageBox.Show("Lỗi dữ liệu load combo khách hàng " + ex.Message);
            }
        }

        private void cbmonan_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadGia();
        }
        public void LoadThongTinHop()
        {
            int lbcom = int.Parse((lbhopcom.Text));
            int lbcanh = int.Parse((lbhopcanh.Text));
            int lbtakem = int.Parse((lbhoptak.Text));
            int lbnuoc = int.Parse((lbhopnuoc.Text));
            HopMonAnBUS hmaBUS = new HopMonAnBUS();
            DataTable dt = new DataTable();
            String maMA = this.cbmonan.SelectedValue.ToString();
            dt = hmaBUS.LayHopMonAnTheoMa2(int.Parse(maMA));

            foreach (DataRow dr in dt.Rows)
            {

                if (dr["ID_Hop"].ToString() == "1")
                {
                    lbcom = int.Parse((lbhopcom.Text)) + 1;
                }
                if (dr["ID_Hop"].ToString() == "2")
                {
                    lbcanh = int.Parse((lbhopcanh.Text)) + 1;
                }
                if (dr["ID_Hop"].ToString() == "3")
                {
                    lbtakem = int.Parse((lbhoptak.Text)) + 1;
                }
                if (dr["ID_Hop"].ToString() == "4")
                {
                    lbnuoc = int.Parse((lbhopnuoc.Text)) + 1;
                }
            }
            lbhopcom.Text = lbcom.ToString();
            lbhopcanh.Text = lbcanh.ToString();
            lbhoptak.Text = lbtakem.ToString();
            lbhopnuoc.Text = lbnuoc.ToString();
        }

        //public void LoadThongTinHop(int chedo)
        //{
        //    int lbcom = int.Parse((lbhopcom.Text));
        //    int lbcanh = int.Parse((lbhopcanh.Text));
        //    int lbtakem = int.Parse((lbhoptak.Text));
        //    int lbnuoc = int.Parse((lbhopnuoc.Text));
        //    HopMonAnBUS hmaBUS = new HopMonAnBUS();
        //    DataTable dt = new DataTable();
        //    String maMA = this.cbmonan.SelectedValue.ToString();
        //    dt = hmaBUS.LayHopMonAnTheoMa2(int.Parse(maMA), chedo);

        //    foreach (DataRow dr in dt.Rows)
        //    {

        //        if (dr["ID_Hop"].ToString() == "1")
        //        {
        //            lbcom = int.Parse((lbhopcom.Text)) + 1;
        //        }
        //        if (dr["ID_Hop"].ToString() == "2")
        //        {
        //            lbcanh = int.Parse((lbhopcanh.Text)) + 1;
        //        }
        //        if (dr["ID_Hop"].ToString() == "3")
        //        {
        //            lbtakem = int.Parse((lbhoptak.Text)) + 1;
        //        }
        //        if (dr["ID_Hop"].ToString() == "4")
        //        {
        //            lbnuoc = int.Parse((lbhopnuoc.Text)) + 1;
        //        }
        //    }
        //    lbhopcom.Text = lbcom.ToString();
        //    lbhopcanh.Text = lbcanh.ToString();
        //    lbhoptak.Text = lbtakem.ToString();
        //    lbhopnuoc.Text = lbnuoc.ToString();
        //}

        private void btthemmon_Click(object sender, EventArgs e)
        {
            
            String mama = cbmonan.SelectedValue.ToString();
            String tenma = cbmonan.Text.ToString();
            string soluong = txtsoluong.Text;
            int n = 0;

            PhieuDatComBUS pdcBUS = new PhieuDatComBUS();
            PhieuDatComDTO pdcDTO = new PhieuDatComDTO();

            int mapdcmax = pdcBUS.LayMaPhieuDatComMax();
            mapdcmax = mapdcmax + 1;

            int soluongton = pdcBUS.TinhSoLuongTon(int.Parse(mama));

            if (txtsoluong.Text == "" || txtgia.Text == "")
            {
                MessageBox.Show("Chưa nhập số lượng");
            }
            else if (!int.TryParse(this.txtsoluong.Text, out n))
            {
                MessageBox.Show("Vui lòng nhập số lượng là số nguyên");
            }
            else if (int.Parse(soluong) > soluongton)
            {
                MessageBox.Show("Số lượng món ăn này còn không đủ");
            }
            else
            {
                String gia = txtgia.Text;
                int thanhtien = 0;
                thanhtien = int.Parse(soluong) * int.Parse(gia);
                this.dgvchitietdatmon.Rows.Add(mapdcmax,mama, tenma, soluong, gia, thanhtien.ToString());
                //LoadThongTinHop(CauHinh.chedo);
                LoadThongTinHop();
                Double result = 0;
                foreach (DataGridViewRow row in this.dgvchitietdatmon.Rows)
                {
                    result += Convert.ToInt32(row.Cells[5].Value);
                }

                this.lbtongtien.Text = result.ToString();
                TinhConLai();
            }
        }
        public void TinhConLai()
        {
            String tong = lbtongtien.Text;

            //int a = int.Parse(lbchieckhau.Text) / 100;
            //int b = int.Parse(lbchieckhau.Text) % 100;
            //String c = a+","+b;

            //Double tongtien = Double.Parse(tong) - (Double.Parse(tong) * Double.Parse(c));
            //lbconlai.Text = tongtien.ToString();
            Double a = Double.Parse(lbchieckhau.Text) / 100;
            Double conlai = Double.Parse(tong) - (Double.Parse(tong) * a);
            lbconlai.Text = conlai.ToString();

        }
        private void btthu_Click(object sender, EventArgs e)
        {
            PhieuDatComBUS pdcBUS = new PhieuDatComBUS();
            string ma = cbkhuyenmai.SelectedValue.ToString();
            DateTime ngay = pdcBUS.LayKhuyenMai(int.Parse(ma));
            MessageBox.Show("Ngay BD "+ngay.ToShortDateString() + "Today "+DateTime.Today );
        }

        private void cbtrangthai_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
            ThemKhachHang fr = new ThemKhachHang();
            fr.ShowDialog();

        }

        private void cbkhuyenmai_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadChiecKhau();
            TinhConLai();
        }
        public void loadChiecKhau()
        {
            KhuyenMaiBUS kmBUS = new KhuyenMaiBUS();
            List<KhuyenMaiDTO> listKM = new List<KhuyenMaiDTO>();
            String maMA = this.cbkhuyenmai.SelectedValue.ToString();
            try
            {
                listKM = kmBUS.LayChiecKhau_theoma(int.Parse(maMA));
                KhuyenMaiDTO maDTO = new KhuyenMaiDTO();

                maDTO = listKM[0];

                //String ID_MonAn = maDTO.ID_MonAn.ToString();
                //String TenMonAn = maDTO.TenMonAn.ToString();
                String Gia = maDTO.ChietKhau.ToString();

                lbchieckhau.Text = Gia;

            }
            catch (Exception ex)
            {
                //MessageBox.Show("Lỗi dữ liệu load chiếc khấu " + ex.Message);
            }
        }

        private void btthemct_Click(object sender, EventArgs e)
        {

        }
        public void ThemCTPC()
        {
            try
            {
                ChiTietPhieuComBUS ctpcBUS = new ChiTietPhieuComBUS();
                string msg = ctpcBUS.ThemChiTietPhieuCom2(dgvchitietdatmon);
                if (msg == "")
                {
                    MessageBox.Show("Thêm chi tiết phiếu thành công !");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi dữ liệu thêm chi tiết phiếu " + ex.Message);
            }
        }

        public void ThemCTPC(int chedo)
        {
            try
            {
                ChiTietPhieuComBUS ctpcBUS = new ChiTietPhieuComBUS();
                string msg = ctpcBUS.ThemChiTietPhieuCom2(dgvchitietdatmon, chedo);
                if (msg == "")
                {
                    MessageBox.Show("Thêm chi tiết phiếu thành công !");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi dữ liệu thêm chi tiết phiếu " + ex.Message);
            }
        }

        private void btxoabo_Click(object sender, EventArgs e)
        {
            int selectedCount = dgvchitietdatmon.SelectedRows.Count;           
            while (selectedCount > 0)
            {
                if (!dgvchitietdatmon.SelectedRows[0].IsNewRow)
                    dgvchitietdatmon.Rows.RemoveAt(dgvchitietdatmon.SelectedRows[0].Index);
                selectedCount--;
            }
        }

        public void CatNhatNguyenLieu()
        {
            try
            {
                KhoThucPhamBUS ktpBUS = new KhoThucPhamBUS();

                string msg = ktpBUS.CapNhatNguyenLieu(dgvchitietdatmon);
                if (msg == "")
                {
                    MessageBox.Show("Cập nhật thành công !");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi dữ liệu cập nhật " + ex.Message);
            }
        }

        public void CatNhatNguyenLieu(int chedo)
        {
            try
            {
                KhoThucPhamBUS ktpBUS = new KhoThucPhamBUS();

                string msg = ktpBUS.CapNhatNguyenLieu(dgvchitietdatmon, chedo);
                if (msg == "")
                {
                    MessageBox.Show("Cập nhật thành công !");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi dữ liệu cập nhật " + ex.Message);
            }
        }

        private void dgvchitietdatmon_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }
            int intIndex = e.RowIndex;
            dgvchitietdatmon.Rows[intIndex].Selected = true;
        }

        private void dgvdsphieudatcom_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }
            int intIndex = e.RowIndex;
            dgvdsphieudatcom.Rows[intIndex].Selected = true;
        }

        private void dgvdsphieudatcom_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int dong = dgvdsphieudatcom.SelectedCells.Count;
            if (dong == 0)
                return;
            dong = dgvdsphieudatcom.SelectedCells[0].RowIndex;
            int _ID_PhieuDatCom = int.Parse(dgvdsphieudatcom.Rows[dong].Cells[0].Value.ToString());
            ChiTietPhieuCom frm = new ChiTietPhieuCom();
            frm.ID_PhieuDatCom = _ID_PhieuDatCom;
            frm.ShowDialog();
        }
    }
}
